// shake.js
var app = getApp()
let that
let userInfo
Page({
  lastTime: 0,
  x: 0,
  y: 0,
  z: 0,
  lastX: 0,
  lastY: 0,
  lastZ: 0,
  shakeSpeed: 130,

  data: {
    loadingOpacity: 0,
    resultOpacity: 0,
    resultTop: 0,
    showTotal: 0,
    total: 0,
    userInfo: {},
    timer: 0,
    countDownNum: 0,
    checkOpen: 1,
    stateText: '还未开始'
  },

  onLoad: function(options) {
    that = this
    userInfo = wx.getStorageSync('userInfo') || false
    if (userInfo) {
      app.reg(userInfo)
      that.setData({
        userInfo: userInfo
      })
    }
    wx.setNavigationBarTitle({
      title: "倩狐互动-摇一摇",
    })
    that.checkOpen()
  },

  onShow: function() {
    wx.startAccelerometer()
    wx.onAccelerometerChange(that.shake)
    that.setData({
      checkOpen: 1
    })
  },

  onUnload: function() {
    wx.stopAccelerometer()
    that.setData({
      checkOpen: 0
    })
  },

  onShareAppMessage: function() {

  },

  checkOpen: function() {
    if (that.data.checkOpen) {
      app.ajax({
        url: "/wechat/config/index",
        data: {
          "item": "shake"
        },
        success: function(json) {
          if (json.res === 1) {
            that.setData({
              countDownNum: 3,
              checkOpen: 0
            })
            setTimeout(() => that.countDown(), 10)
          } else {
            setTimeout(() => that.checkOpen(), 3000)
          }
        }
      })
    }
  },

  shake: function(res) {
    let nowTime = new Date().getTime()
    if (nowTime - that.lastTime > 10) {
      let diffTime = nowTime - that.lastTime
      that.lastTime = nowTime
      that.x = res.x
      that.y = res.y
      that.z = res.z
      let speed = Math.abs(that.x + that.y + that.z - that.lastX - that.lastY - that.lastZ) / diffTime * 10000
      if (speed > that.shakeSpeed) {
        console.log("speed：" + speed)
        that.showResult()
      }
      that.lastX = that.x
      that.lastY = that.y
      that.lastZ = that.z
    }
  },

  showResult: function() {
    let maxTotal = 430
    let countDownNum = that.data.countDownNum
    if (countDownNum === 0) {
      let total = that.data.total + 1
      if (total <= maxTotal) {
        if (that.data.resultOpacity === 0) {
          that.setData({
            loadingOpacity: 1,
            resultOpacity: 0,
            resultTop: 0,
            total: total,
            showTotal: parseInt(total * 100 / maxTotal)
          })
          setTimeout(function() {
            that.setData({
              loadingOpacity: 0,
              resultOpacity: 1,
              resultTop: 50
            })
          }, 1000)
        } else {
          that.setData({
            total: total,
            showTotal: parseInt(total * 100 / maxTotal)
          })
          if (total % 10 === 0) {
            console.log(total)
            that.setData({
              loadingOpacity: 1
            })
            app.ajax({
              url: "/wechat/climbTree/update",
              data: {
                "openId": userInfo.openId,
                "times": total
              },
              success: function(json) {
                if (json.res === 1) {
                  that.setData({
                    loadingOpacity: 0
                  })
                } else {
                  that.setData({
                    countDownNum: 0,
                    checkOpen: 1
                  })
                  that.checkOpen()
                }
              }
            })
            setTimeout(function() {
              that.setData({
                loadingOpacity: 0
              })
            }, 2000)
          }
        }
      } else {
        wx.showLoading({
          title: '本轮结束了',
          duration: 2000
        })
      }
    } else {
      wx.showLoading({
        title: '亲别急啊',
        duration: 2000
      })
    }
  },
  countDown: function() {
    let countDownNum = that.data.countDownNum
    that.setData({
      timer: setInterval(function() {
        countDownNum--
        that.setData({
          countDownNum: countDownNum
        })
        if (countDownNum == 0) {
          clearInterval(that.data.timer)
          that.setData({
            timer: 0,
            stateText: '赶紧摇起来吧'
          })
        }
      }, 1000)
    })
  }

})